<?php
// check for cached file
define('CACHE', 'cache/basic_stats.html');
define('CACHE_UPDATE', 15); // in minutes

if (file_exists(CACHE) && (filemtime(CACHE) > (time()-(CACHE_UPDATE*60)))) { // exists & newer than XXmins?
	$page = file_get_contents(CACHE);
	echo $page;
	echo "<div id='cache_status'>This stats page is CACHED! It will be refreshed in ".ceil((filemtime(CACHE)-(time()-(CACHE_UPDATE*60)))/60)." minutes.</div>";
	echo "\t</body>\n";
	echo "</html>";
	exit;
}

function write_cache($buffer) {
	$fp = fopen(CACHE, 'w');
	if (!$fp) {
		return 1;
	}
	fwrite($fp, $buffer);
	fclose($fp);
}

// ok, no cache or out of date, recreate and then output.
touch(CACHE); // so only we rewrite the cache (hopefully)
ob_start();
?>
<html>
	<head>
		<title>Thrissa's siggen - Basic Stats</title>
	</head>
	<style type="text/css>
		#cache_status {
			background-color: #ccc;
			margin-top: 2em;
			text-align: center;
			padding: 2px;
		}
	</style>
	<body>
	<h1>Thrissa's siggen - Basic Stats</h1>
	<?php
	echo "<fieldset>\n";
	echo "<legend>Cache Stats</legend>\n";
		$ussize = 0;
		$cache = realpath('.').'/cache';
		$dh = opendir($cache.'/us');
		$usservers = array();
		while (false !== ($servername = readdir($dh))) {
			if ($servername != '..' && $servername != '.') {
	   		$usservers[$servername] = 0;
	   	}
		}
		closedir($dh);
		$usnumchars = 0;
		foreach ($usservers as $servername => $num) {
			$numfiles = 0;
			$dh = opendir($cache.'/us/'.$servername);
			while (false !== ($char = readdir($dh))) {
				if ($char != '..' && $char != '.') {
					$numfiles++;
					$ussize += filesize($cache.'/us/'.$servername.'/'.$char);
				}
			}
			$usnumchars += $numfiles;
			$usservers[$servername] = $numfiles;
		}
		closedir($dh);
		arsort($usservers);
		echo "<h3>US Stats</h3>\n";
		echo "Servers: ".count($usservers)."/222<br />\n";
		echo "Characters: ".$usnumchars."<br />\n";
		echo "XML Filesize: ".$ussize." bytes. (".round($ussize/1024/1024, 2)."MB)<br />\n";
		echo "Most popular: ";
		$i = 0;
		foreach ($usservers as $servername => $num) {
			echo $servername.': '.$num.', ';
			$i++;
			if ($i >= 5) break;
		}
		
		/* EU stats */
		$cache = realpath('.').'/cache';
		$dh = opendir($cache.'/eu');
		$euservers = array();
		$eusize = 0;
		while (false !== ($servername = readdir($dh))) {
			if ($servername != '..' && $servername != '.') {
	   		$euservers[$servername] = 0;
	   	}
		}
		closedir($dh);
		$eunumchars = 0;
		foreach ($euservers as $servername => $num) {
			$numfiles = 0;
			$dh = opendir($cache.'/eu/'.$servername);
			while (false !== ($char = readdir($dh))) {
				if ($char != '..' && $char != '.') {
					$numfiles++;
					$eusize += filesize($cache.'/eu/'.$servername.'/'.$char);
				}
			}
			$eunumchars += $numfiles;
			$euservers[$servername] = $numfiles;
		}
		closedir($dh);
		arsort($euservers);
		echo "<h3>EU Stats</h3>\n";
		echo "Servers: ".count($euservers)."/235<br />\n";
		echo "Characters: ".$eunumchars."<br />\n";
		echo "XML Filesize: ".$eusize." bytes. (".round($eusize/1024/1024, 2)."MB)<br />\n";
		echo "Most popular: ";
		$i = 0;
		foreach ($euservers as $servername => $num) {
			echo $servername.': '.$num.', ';
			$i++;
			if ($i >= 5) break;
		}
		
		echo "<h3>Total stats</h3>";
		echo "Servers: ".(count($usservers)+count($euservers))."/457<br />\n";
		echo "Characters: ".($usnumchars+$eunumchars)."<br />\n";
		
		echo "</fieldset>\n";
	?>
<?php
write_cache(ob_get_contents());
?>
	<div id='cache_status'>This stats page is non-cached!</div>
	</body>
</html>